package com.winning.sjzc.zyml.utils;

import com.winning.sjzc.zyml.entity.PsmsSjjflEntity;
import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SjjflTreeUtil
 * @Description 构造树
 * @Author WJX
 * @Date 2020/3/2 19:36
 **/
@Data
public class SjjflTreeUtil {

    public List<PsmsSjjflEntity> flCommon;
    public List<Object> list = new ArrayList<Object>();

    public List<Object> sjjflTreeList(List<PsmsSjjflEntity> flList,String fldm){
        this.flCommon = flList;
        for (PsmsSjjflEntity ste : flList) {
            Map<String,Object> mapArr = new LinkedHashMap<>();
            if("1".equals(ste.getFljb())){
                mapArr.put("id", ste.getSjjfldm());
                mapArr.put("name", ste.getSjjflmc());
                mapArr.put("pId", ste.getSjjfldm());
                mapArr.put("fljb", ste.getFljb());
                list.add(mapArr);
                list.addAll(jgYjChild(ste.getSjjfldm(),"2"));
            }
        }
        return list;
    }

    /**
     * 拼接一级树
     * @param fldm
     * @param fljb
     * @return
     */
    private List<?> jgYjChild(String fldm,String fljb){
        List<Object> lists = new ArrayList<>();
        for(PsmsSjjflEntity a : flCommon){
            Map<String,Object> childArray = new LinkedHashMap<>();
            if(null != a.getYjlbdm() && a.getYjlbdm().equals(fldm) && fljb.equals(a.getFljb())){
                childArray.put("id", a.getSjjfldm());
                childArray.put("name", a.getSjjflmc());
                childArray.put("pId", a.getYjlbdm());
                childArray.put("fljb", a.getFljb());
                lists.add(childArray);
                lists.addAll(jgEjChild(a.getSjjfldm(),"3"));
            }
        }
        return lists;
    }

    /**
     * 拼接二级树
     * @param fldm
     * @param fljb
     * @return
     */
    private List<?> jgEjChild(String fldm,String fljb){
        List<Object> lists = new ArrayList<>();
        for(PsmsSjjflEntity a : flCommon){
            Map<String,Object> childArray = new LinkedHashMap<>();
            if(null != a.getEjlbdm() && a.getEjlbdm().equals(fldm) && fljb.equals(a.getFljb())){
                childArray.put("id", a.getSjjfldm());
                childArray.put("name", a.getSjjflmc());
                childArray.put("pId", a.getEjlbdm());
                childArray.put("fljb", a.getFljb());
                lists.add(childArray);
            }
        }
        return lists;
    }

}
